package com.dq.rbac.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dq.rbac.entity.SysMenu;
import com.dq.rbac.entity.SysRole;
import com.dq.rbac.entity.SysUserRole;
import com.dq.rbac.service.SysMenuService;
import com.dq.rbac.vo.R;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @description: 系统菜单控制器
 * @author: rbac_小强 (QQ:2680825683)
 * @className: SysMenuController
 * @since: 2023/1/28 16:49
 */
@RestController
@RequestMapping("/menu")
public class SysMenuController {
    @Resource
    private SysMenuService menuService;

    /**
     * 查询所有菜单树信息
     *
     * @return
     */
    @RequestMapping("treeList")
    @PreAuthorize("hasAuthority('system:menu:query')")
    public R treeList() {
        List<SysMenu> menuList = menuService.list(new LambdaQueryWrapper<SysMenu>().orderByAsc(SysMenu::getOrderNum));
        //把MenuList转化为Tree
        List<SysMenu> treeMenu = menuService.buildTreeMenu(menuList);
        return R.ok().data("treeMenu", treeMenu);
    }


    /**
     * 根据id是否提供来判断添加或者修改
     * 权限：add edit
     *
     * @param sysMenu
     * @return
     */
    @PostMapping("save")
    @PreAuthorize("hasAuthority('system:menu:add')" + "||" + "hasAuthority('system:menu:edit')")
    public R save(@RequestBody SysMenu sysMenu) {
        if (sysMenu.getId() == null) { //添加
            System.out.println("sysMenu null = " + sysMenu);
            sysMenu.setCreateTime(new Date());
            menuService.save(sysMenu);
            return R.ok().message("角色添加成功");
        }
        //修改
        System.out.println("sysMenu update = " + sysMenu);
        sysMenu.setUpdateTime(new Date());
        boolean success = menuService.updateById(sysMenu);
        if (success) {
            return R.ok().message("更新成功");
        } else {
            return R.error().message("修改失败");
        }
    }

    /**
     * 根据id查询菜单
     * 用于回显修改时的数据
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('system:menu:query')")
    public R queryById(@PathVariable("id") Long id) {
        SysMenu sysMenu = menuService.getById(id);
        return R.ok().data("sysMenu", sysMenu);
    }


    /**
     * 删除菜单
     * 判断是否含有父节点
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('system:menu:delete')")
    @Transactional
    public R delete(@PathVariable(value = "id") Long id) {
        //查询当前id下是否有其他子节点 如果有则无法删除
        int childrenCount = menuService.count(new LambdaQueryWrapper<SysMenu>().eq(SysMenu::getParentId, id));
        if (childrenCount > 0) {
            //有子节点
            return R.error().message("删除失败，请检查是否有子元素未删除");
        }
        menuService.removeById(id);
        return R.ok().message("删除成功");
    }
}
